package com.gxa.dao.impl;

import com.gxa.common.Constants;
import com.gxa.dao.RoleDao;
import com.gxa.dbutils.JdbcBuild;
import com.gxa.pojo.Department;
import com.gxa.pojo.Role;

import java.sql.SQLException;
import java.util.List;

public class RoleDaoImpl extends JdbcBuild implements RoleDao {

    @Override
    public List<Role> queryRoleList(Integer p1, Integer p2) throws SQLException {
        String sql = "select *,update_time as tt from role where del_status=? limit ?,?";
        Object[] parmar={Constants.Del_STATUS_OK,p1,p2};
        return super.queryListEntity(Role.class,sql,parmar);
    }

    @Override
    public List<Role> queryRoleList(Integer p1, Integer p2, String role_name) throws SQLException {
        String sql = "select *,update_time as tt from role where role_name=? and del_status=? limit ?,?";
        Object[] params = {role_name,Constants.Del_STATUS_OK,p1,p2};
        return super.queryListEntity(Role.class,sql,params);
    }

    @Override
    public Department getDepartment(Integer departmet_id) throws SQLException {
        String sql = "select * from department where id = ?";
        return super.queryEntity(Department.class,sql,departmet_id);
    }

    @Override
    public Integer countRole() throws SQLException {
        String sql = "select count(*) from role where del_status = ?";

        return super.findAllForInt(sql,Constants.Del_STATUS_OK);
    }

    @Override
    public Integer countRole(String role_name) throws SQLException {
        String sql = "select Count(*) from role where del_status =? and role_name=?";
        Object[] params = {Constants.Del_STATUS_OK,role_name};
        return super.findAllForInt(sql,params);
    }

    @Override
    public Integer delRoleById(String id) throws SQLException {
        String sql = "update role set del_status=? where id=?";
        Object[] params = {Constants.Del_STATUS_DEL,id};
        return super.executeUpdate(sql,params);
    }

    @Override
    public Integer addRole(Role role) throws SQLException {
        String sql = "insert into role(role_name,role_num,department_id,update_time,mark,del_status) values (?,?,?,?,?,?)";
        Object[] params={role.getRole_name(),role.getRole_num(),role.getDepartment_id(),role.getUpdate_time(),role.getMark(),Constants.Del_STATUS_OK};
        return super.executeUpdate(sql,params);
    }

    @Override
    public Integer updateRole(Role role) throws SQLException {
        String sql = "update role set role_name=?,role_num=?,department_id=?,mark=?,update_time=? where id=?";
        Object[] params = {role.getRole_name(),role.getRole_num(),role.getDepartment_id(),role.getMark(),role.getUpdate_time(),role.getId()};

        return super.executeUpdate(sql,params);
    }
}
